(function() {
    'use strict';

    /**
     * @ngdoc service
     * @name webApp.myrest
     * @description
     * # myrest
     * Service in the webApp.
     */
    angular.module('webApp')
        .config(function(CacheFactoryProvider) {
            angular.extend(CacheFactoryProvider.defaults, { maxAge: 15 * 60 * 1000 });
        })
        .service('cacheService', function(CacheFactory) {

            function myCache() {
                var a = {};

                //初始化
                function cserver(keyName, ttl) {
                    if (!CacheFactory.get(keyName)) {
                        // or CacheFactory('kvCache', { ... });
                        if (ttl) {
                            ttl = ttl * 1000;
                        } else {
                            ttl = 15 * 24 * 60 * 60 * 1000;
                        }
                        CacheFactory(keyName, {
                            maxAge: ttl, // 单个过期时间 Items added to this cache expire after 15 minutes.
                            cacheFlushInterval: 15 * 24 * 60 * 60 * 1000, // 该时间后删除所有 This cache will clear itself every hour.
                            deleteOnExpire: 'aggressive', // Items will be deleted from this cache right when they expire.
                            storageMode: 'localStorage' // This cache will use `localStorage`.
                        });
                    }
                    return CacheFactory.get(keyName);
                }

                //写入
                a.set = function(keyName, val, ttl) {
                    cserver(keyName, ttl).put(keyName, JSON.stringify(val));
                    //console.log("保存：",keyName,"11111111",CacheFactory.get(keyName),CacheFactory.get(keyName).get(keyName));
                }

                //查询
                a.get = function(keyName, ttl) {
                    var cstr = cserver(keyName, ttl).get(keyName);
                    // console.log('cstr....', cstr);
                    if (cstr) {
                        return JSON.parse(cstr);
                    } else {
                        // console.log("cache没内容");
                    }
                }
                //删除
                a.del = function(keyName) {
                    cserver(keyName, 0).remove(keyName);
                }
                //销毁所有
                a.destroy = function() {
                    localStorage.clear();
                    // CacheFactory.clearAll();
                }

                return a;
            }

            return myCache();
        })
        .service('sessionService', function(CacheFactory) {
            if (!CacheFactory.get('dhfErpSession')) {
                // or CacheFactory('dhfErpSession', { ... });
                CacheFactory('dhfErpSession', {
                    maxAge: 24 * 60 * 60 * 1000, // Items added to this cache expire after 15 minutes.
                    cacheFlushInterval: 24 * 60 * 60 * 1000, // This cache will clear itself every minutes.
                    deleteOnExpire: 'aggressive', // Items will be deleted from this cache right when they expire.
                    storageMode: 'sessionStorage' // This cache will use `sessionStorage`.
                });
            }

            return CacheFactory.get('dhfErpSession');
        });
})();
